package com.xgb.edu;

import java.util.Stack;


public class Bracket {

	/**
	 * 检查infix表达式中的圆括号是否匹配，若匹配，返回空字符串；否则返回错误信息
	 * @param infix 需要验证的表达式
	 * @return
	 */
	public static String isMatched(String infix) {
		Stack<String> stack = new Stack<>();
		for (int i = 0; i < infix.length(); i++) {
			char ch = infix.charAt(i);
			switch(ch) {
				case '(' : stack.push(ch + "'");
				case ')' : if(stack.isEmpty() || stack.pop().equals("("))
					return "期望(";
			}
		}
		
		return stack.isEmpty() ? "" : "期望)";
	}
	
	public static void main(String[] args) {
		String infix = "((1+2)*3+4)(";
		System.err.println(infix + "  ,编译错误： " + Bracket.isMatched(infix));
	}
}
